﻿// -------------------------------------------------------------------------
//    @FileName         :    NFActivityEightPart.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-30
//    @Email            :    445267987@qq.com
//    @Module           :    NFActivityEightPart
//
// -------------------------------------------------------------------------

#include "NFActivityEightPart.h"

#include <ActivityEightDescEx.h>
#include <NFLogicConfig.h>

#include "ConstantDescEx.h"
#include "NFPlayer.h"
#include "proto_db.pb.h"

NFActivityEightPart::NFActivityEightPart()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFActivityEightPart::~NFActivityEightPart()
{
}

int NFActivityEightPart::CreateInit()
{
    return 0;
}

int NFActivityEightPart::ResumeInit()
{
    return 0;
}


int NFActivityEightPart::LoadFromDB(const proto_ff::tbRoleDetail* pDBData)
{
    m_stData.FromPb(pDBData->role_play_data().act_eight_data());
    return 0;
}

int NFActivityEightPart::InitConfig(proto_ff::tbRoleDetail* pDbData, const RoleOne& stRoleOne)
{
    auto pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_LOGIC_SERVER);
    CHECK_NULL(0, pConfig);
    CHECK_NULL(0, pDbData);
    auto pData = pDbData->mutable_role_play_data()->mutable_act_eight_data();
    CHECK_NULL(0, pData);
    pData->set_cur_score(0);

    int iCRTime = NF_ADJUST_TIMENOW();
    int iServerOpenTime = pConfig->GetServerOpenTime();
    int iActivityStartTime = 0;
    int iGroupID = ActivityEightDescEx::Instance()->GetGroupID(iCRTime, iServerOpenTime, NF_ADJUST_TIMENOW(), iActivityStartTime);
    pData->set_activity_group(iGroupID);
    pData->set_activity_start_time(iActivityStartTime);

    return 0;
}

int NFActivityEightPart::SaveDB(proto_ff::tbRoleDetail* pDBData)
{
    m_stData.ToPb(pDBData->mutable_role_play_data()->mutable_act_eight_data());
    return 0;
}

int NFActivityEightPart::MakePlayerDataCS(proto_ff::Proto_CSGetRoleRsp* pCSGetRoleRsp)
{
    return 0;
}

int NFActivityEightPart::MakeCityPlayerData(proto_ff::CityRoleInfo* pGCCityRoleInfo)
{
    return 0;
}

int NFActivityEightPart::DayTick()
{
    if (IsInActivityTime())
    {
        return 0;
    }

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    auto pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_LOGIC_SERVER);
    CHECK_NULL(0, pConfig);

    int iCRTime = pPlayer->GetRoleBase()->create_time;
    int iServerOpenTime = pConfig->GetServerOpenTime();
    int iActivityStarTime = 0;
    int iGroupID = ActivityEightDescEx::Instance()->GetGroupID(iCRTime, iServerOpenTime, NF_ADJUST_TIMENOW(), iActivityStarTime);

    if (iGroupID != m_stData.activity_group)
    {
        m_stData.activity_group = iGroupID;
        m_stData.activity_start_time = iActivityStarTime;
        m_stData.cur_score = 0;
        m_stData.data.clear();;
        m_stData.score_rwd.clear();

        MarkDirty();
    }
    return 0;
}

bool NFActivityEightPart::IsInActivityTime()
{
    int iDayTime = ConstantDescEx::Instance()->GetValue(proto_ff::EN_CONST_ACTIVITY_EIGHT_ONLINE_DAY);
    if (m_stData.activity_group > 0)
    {
        int iElapseTime = NF_ADJUST_TIMENOW() - m_stData.activity_start_time;
        if (iElapseTime < iDayTime)
        {
            return true;
        }
    }

    return false;
}
